import re


def getCurrentSingleRoundDialogue(dialogue_session):
    dialogueItemList = getAllDialogueHistory(dialogue_session)
    if dialogueItemList is None:
        return None
    return dialogueItemList[len(dialogueItemList)-1]


def getAllDialogueHistory(dialogue_session):
    if dialogue_session is None or "dialogueHistory" not in dialogue_session \
            or "dialogueItemList" not in dialogue_session["dialogueHistory"]:
        return None
    dialogueHistory = dialogue_session["dialogueHistory"]
    if "dialogueItemList" in dialogueHistory:
        return dialogueHistory["dialogueItemList"]
    return None


# 根据对话历史获取标签
def get_all_labels(dialogueItemList):
    if dialogueItemList is None or len(dialogueItemList) < 1:
        return None
    contentLabels = []
    for dialogueItem in dialogueItemList:
        reply = dialogueItem['reply']
        if reply is not None and reply != "":
            pattern = r'\[(.*?)\]'  # 匹配[]中的内容
            labels = re.findall(pattern, reply)
            for label in labels:
                contentLabels.append(label)
    return contentLabels


def setReply(reply, dialogue_session):
    singleRoundDialogue = getCurrentSingleRoundDialogue(dialogue_session)
    if singleRoundDialogue is None:
        return None
    singleRoundDialogue["reply"] = reply


def getAnswer(self):
    return self.getAnswer()


if __name__ == '__main__':
    # dialogue_session = DialogueSession(workspace_id=661490)
    # dialogueHistory = dialogue_session.getAllDialogueHistory()
    # reply = "[京东养老金带操作B版-0706]@#开场-李先生||喂，您好~请问您是李先生吗？#@"
    # workspaceId = 661490
    # dialogueSingle = DialogueSingle(reply=reply, workspace_id=workspaceId)
    # dialogueHistory.get("dialogueItemList").append(dialogueSingle)
    # print(json.dumps(dialogue_session.__dict__))
    pass
